package com.mars.admin.modules.admin;

import com.mars.admin.common.group.AddGroup;
import com.mars.admin.common.group.UpdateGroup;
import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.banner.entity.Banner;
import com.mars.admin.modules.banner.service.BannerService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("admin/banner")
@Tag(name = "轮番图管理", description = "轮番图相关接口")
@AllArgsConstructor
public class AdminBannerController {
    
    @Autowired
    private BannerService bannerService;
    
    /**
     * 新增轮番图
     */
    @PostMapping("/add")
    @Operation(summary = "新增轮番图", description = "新增轮番图")
    public Result<Void> add(@Validated(AddGroup.class) @RequestBody Banner banner) {
        bannerService.add(banner);
        return Result.success();
    }


    /**
     * 更新轮番图
     */
    @PostMapping("/update")
    @Operation(summary = "更新轮番图", description = "更新轮番图")
    public Result<Void> update(@Validated(UpdateGroup.class) @RequestBody Banner banner) {
        bannerService.updateCategory(banner);
        return Result.success();
    }


    /**
     * 删除轮番图
     */
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除轮番图", description = "删除轮番图")
    public Result<Void> delete(@PathVariable Long id) {
        bannerService.delete(id);
        return Result.success();
    }

    /**
     * 获取轮廓图信息
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "获取轮廓图信息", description = "获取轮廓图信息")
    public Result<Banner> get(@PathVariable Long id) {
        return Result.success(bannerService.getById(id));
    }

    /**
     * 分页查询轮播图
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询轮播图", description = "分页查询轮播图列表")
    public Result<Page<Banner>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "轮播图标题") @RequestParam(required = false) String title,
            @Parameter(description = "轮播图类型") @RequestParam(required = false) String type,
            @Parameter(description = "展示位置") @RequestParam(required = false) String position,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        Page<Banner> page = Page.of(current, size);
        Page<Banner> result = bannerService.pageBanner(page, title, type, position, status);
        return Result.success(result);
    }

    /**
     * 批量删除轮播图
     */
    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除轮播图", description = "批量删除轮播图")
    public Result<Void> batchDelete(@RequestBody Map<String, List<Long>> request) {
        List<Long> ids = request.get("ids");
        bannerService.batchDelete(ids);
        return Result.success();
    }
}
